/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import com.ebay.services.finding.SearchItem;
import entities.Acheteur;
import entities.Article;
import entities.Magasin;
import entities.Vendeur;
import java.util.ArrayList;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Lionel Prado
 */
@Stateless
@LocalBean
public class GestionnaireArticle {

    @PersistenceContext(unitName = "webServiceProjectBoosGiroud2013-ejbPU")
    private EntityManager em;

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public void enregistrerArticle(Magasin m, Article a) {
        if (m == null) {
            new Exception("magasin null");
        }

        System.out.println("------------------------- Article ephemere : " + a.getNom());

        a.setMagasin(m);
        persist(a);
        m.getListeArticle().add(a);
        em.merge(m);
        
    }

    public void persist(Article object) {
        em.persist(object);
    }

    public void persist(Magasin object) {
        em.persist(object);
    }

    public void creerArticle(Vendeur v) {

        Article a = new Article("aaa", 100L, "");

    }

    public Collection<Article> getListeArticlePourUnVendeur(Vendeur v) {
        Collection<Article> listing = new ArrayList<Article>();
        for (Magasin m : v.getListeMagasins()) {
            for (Article a : m.getListeArticle()) {
                listing.add(a);
            }
        }
        return listing;
    }
// retourner le listing des article

    public Collection<Article> ListeArticlePourUnMagasin(Long id) {

        Query query = em.createNamedQuery("Article.findArticlesPourUnMagasin");
        query.setParameter("idMagasinCourant", id);
        return query.getResultList();

    }

    public Magasin getMagasinById(Long id) {
        System.out.println("recherche en cours");
        return em.find(Magasin.class, id);
    }

    public void CreationJeuxDonneArticle(Magasin m) {
        String[] key = new String[6];
        key[0] = "Asus";
        key[1] = "Simpsons";
        key[2] = "Lumia";
        key[3] = "Iphone";
        key[4] = "Ipad";
        key[5] = "Galaxie";
       
            int val = (int) (Math.random() * 6);

            Collection<Article> listeA = GestionnaireWebService.rechercherArticle(key[val]);
            ArrayList<Article> listeAarray = new ArrayList<Article>(listeA);
            for (int j = 0; j < 20; j++) {
                int valArticle = (int) (Math.random() * listeAarray.size() - 1);
                enregistrerArticle(m, listeAarray.get(valArticle));
            }
        

    }
    //suprimer un article d'un magasin
    //rechercher article par son nom

    public void deleteArticle(Article article) {
        em.remove(em.find(Article.class, article.getId()));
    }

    public void deleteMagasin(Magasin magasin) {
        em.remove(em.find(Magasin.class, magasin.getId()));
    }

    public Collection<Article> getAllArticle() {
        Query query = em.createNamedQuery("Article.findAll");
        return query.getResultList();
    }
    
    public Article getArticle(Long id) {
        System.out.println("recherche en cours");
        return em.find(Article.class, id);
    }
    

    public Collection<Article> recupererArticleParMotCle(String mots) {
        Collection<Article> articlesPotentiel = getAllArticle();
        Collection<Article> articles = new ArrayList<Article>();

        for (Article a : articlesPotentiel) {
            if (a.getNom().contains(mots)) {
                articles.add(a);
            }
        }
        return articles;
    }
    
    public Article update(Article a) {
//        System.out.println("cette fois on est ds gestionnaire..."); 
        return em.merge(a);  
    }
        
}
